package com.miszczu.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Repository;

import com.miszczu.model.Classes;
import com.miszczu.model.Users;
import com.miszczu.utils.CustomHibernateSupport;

@Repository
public class ClassesDaoImpl extends CustomHibernateSupport implements ClassesDao {

	public void save(Classes classes) {
		
		getHibernateTemplate().save(classes);

	}

	public List<Classes> getAllClasses() {
		
		return (List<Classes>) getSession().createQuery("from Classes c order by c.className").list();
		
	}

	public Classes getClassById(Integer id) {
		
		return (Classes) getHibernateTemplate().get(Classes.class, id);
		
	}

	public void delete(Classes classes) {

		getHibernateTemplate().delete(classes);
		
	}

	public void update(Classes classes) {
		
		getHibernateTemplate().update(classes);
		
	}

	public boolean isSubjectInClass(Integer classId, Integer subjectId) {
		
		Integer[] args = {subjectId, classId };
		
		
		List<Object[]> results = getHibernateTemplate().find("from Classes as c inner join c.subjectses as s where s.subjectId = ? and c.classId = ?", args);
		
		if(results.isEmpty()) {
			return false;
		}
		
		return true;
	}


}
